package com.jwdf.uda.ui.fragment.find;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.FilterListEvent;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.CollocationListAction;
import com.jwdf.uda.ui.adapter.RecommendAdapter;
import com.jwdf.uda.ui.adapter.item.HomeRecommendItem;
import com.jwdf.uda.ui.fragment.base.SimpleListFragment;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.EventBusUtil;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/5/10.
 */
public class SquareFragment extends SimpleListFragment {
    private RecommendAdapter mRecommendAdapter;
    private PbBean.PbSignPageFilterType mFilterType = PbBean.PbSignPageFilterType.PAGE_FILTER_TYPE_NONE;

    public static SquareFragment instance() {
        SquareFragment view = new SquareFragment();
        return view;
    }

    @Override
    protected void initViews() {
        mRecommendAdapter = new RecommendAdapter(getActivity(),true);
        mDataBinding.commonFragmentList.setAdapter(mRecommendAdapter);
        EventBusUtil.registerEvent(mRecommendAdapter);
    }

    @Override
    protected void onListItemClick(int position) {
    }

    @Override
    protected void requestData(final boolean refresh) {
        if (refresh && mRecommendAdapter.isEmpty()) {
            showProgressDialog();
        }
        int offset = refresh ? 0 : mRecommendAdapter.getCount();
        onNewRequestCall(CollocationListAction.newInstance(getContext(), getUserId(), offset, getFilterType())
                                 .request(new AHttpService.IResCallback<PbBean.PbResGuideGetGuideList>() {
                                     @Override
                                     public void onCallback(int resultCode, Response<PbBean.PbResGuideGetGuideList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                                         if (getView() != null) {
                                             closeProgressDialog();
                                             if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                                                 mDataBinding.commonFragmentList.stopRefresh();
                                                 mDataBinding.commonFragmentList.stopLoadMore();
                                                 return;
                                             }
                                             doSuccessResponse(refresh, response.body());
                                         }
                                     }
                                 }));
    }
    protected PbBean.PbSignPageFilterType getFilterType() {
        return mFilterType;
    }
    protected String getUserId() {
        return ConfigUtils.getUserId(getContext());
    }
    private void doSuccessResponse(boolean refresh, PbBean.PbResGuideGetGuideList body) {
        if (body != null) {
            if (refresh) {
                mRecommendAdapter.setData(HomeRecommendItem.convert(body.getGuideListList()));
            } else {
                mRecommendAdapter.append(HomeRecommendItem.convert(body.getGuideListList()));
            }
            if (body.getGuideListList().size() == 0) {
                mDataBinding.commonFragmentList.setEmptyView(mDataBinding.tvEmptyData);
            }
            mDataBinding.commonFragmentList.setPullLoadEnable(body.getGuideListList().size() == getPageLength());
            mDataBinding.commonFragmentList.stopRefresh();
            mDataBinding.commonFragmentList.stopLoadMore();
        }
    }
    @Override
    public void onStart() {
        super.onStart();
        EventBusUtil.registerEvent(this);
    }
    @Override
    public void onStop() {
        super.onStop();
        EventBusUtil.unregisterEvent(this);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBusUtil.unregisterEvent(mRecommendAdapter);
    }
    public void onEvent(IndividualEvent event) {
        autoRefresh();
    }
    public void onEvent(FilterListEvent event) {
        mFilterType = event.menuItem == 0 ? PbBean.PbSignPageFilterType.PAGE_FILTER_TYPE_NONE : PbBean.PbSignPageFilterType.PAGE_FILTER_TYPE_FOLLOW;
        autoRefresh();
    }
}
